<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="h2">开始跑起来！</button>
  <img src="self_emotion_crazy_1.png"/>
  <script>
    window.onload = function(){
      let h2 = document.querySelector("button")
      let img = document.querySelector("img")

      h2.onclick = function(){
        // changeImg(img,"self_emotion_crazy_",16,100)
        changeImg({
          img,
          src: "self_emotion_crazy_",
          length: 16,
          speed: 100
        })

      }
      // function changeImg(){
      //   let timer = null
      //   let i = 1
      //   timer && window.clearInterval(timer)
      //   timer = window.setInterval(()=>{
      //     console.log(i);
      //     let src = `self_emotion_crazy_${i}.png`;
      //     img.src = src;
      //     if(i>=16){
      //       window.clearInterval(timer)
      //       timer = null
      //     }else{
      //       i++
      //     }
      //   },500)
      // }
      function changeImg(obj,img,src,length,speed){
        let timer = null;
        let i = 1;
        timer = window.setInterval(()=>{
          obj.img.src = obj.src + i + ".png";
          i >= obj.length ? window.clearInterval(timer) : i++;
        },obj.speed)
      }
    }
  </script>
</body>
</html>